﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLiNhaSachBUS;
using QuanLiNhaSachDTO;

namespace QuanLiNhaSachGUI
{
    public partial class frmQuanLiNhaCungCap : Form
    {
        public frmQuanLiNhaCungCap()
        {
            InitializeComponent();
        }

        private void frmQuanLiNhaCungCap_Load(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void LoadGrid()
        {
            DataTable dt = NhaCungCapBUS.SelectAll();
            int i = 0;
            dataGridView1.Rows.Clear();
            foreach (DataRow row in dt.Rows)
            {
                dataGridView1.Rows.Add();
                DataGridViewRow dgvr = dataGridView1.Rows[i++];
                dgvr.Cells["Ten"].Value = row["Ten"].ToString();
                dgvr.Cells["ID"].Value = row["id"].ToString();
                dgvr.Cells["Email"].Value = row["Email"].ToString();
                dgvr.Cells["DienThoai"].Value = row["DienThoai"].ToString();
                dgvr.Cells["DiaChi"].Value = row["DiaChi"].ToString();
                dgvr.Cells["GhiChu"].Value = row["GhiChu"].ToString();
                dgvr.Cells["Check"].Value = false;
            }
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells["Check"].Value != null && row.Cells["Check"].Value.ToString() == "True")
                {
                    NhaCungCap q = new NhaCungCap();
                    q.Ten = row.Cells["Ten"].Value.ToString();
                    q.Email = row.Cells["Email"].Value.ToString();
                    q.DienThoai = row.Cells["DienThoai"].Value.ToString();
                    q.DiaChi = row.Cells["DiaChi"].Value.ToString();
                    q.GhiChu = row.Cells["GhiChu"].Value.ToString();
                    if (row.Cells["ID"].Value == null)
                        NhaCungCapBUS.Insert(q);
                    else
                    {
                        q.Id = row.Cells["ID"].Value.ToString();
                        NhaCungCapBUS.Update(q);
                    }
                }
            }
            LoadGrid();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            string s = "\n";
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells["Check"].Value != null && row.Cells["Check"].Value.ToString() == "True" && row.Cells["ID"].Value != null)
                    s += "- " + row.Cells["Ten"].Value.ToString() + "\n";
            }
            if (s.Length > 1)
            {
                DialogResult r = MessageBox.Show("Xóa các Nhà Cung Cấp: " + s, "Xóa Nhà Cung Cấp", MessageBoxButtons.OKCancel);
                if (r == DialogResult.Cancel)
                    return;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells["Check"].Value != null && row.Cells["Check"].Value.ToString() == "True" && row.Cells["ID"].Value != null)
                    {
                        TheLoai q = new TheLoai();
                        q.Id = row.Cells["ID"].Value.ToString();
                        TheLoaiBUS.Delete(q);
                    }
                }
                LoadGrid();
            }
        }

        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 0)
                return;
            dataGridView1.Rows[e.RowIndex].Cells["Check"].Value = true;
        }

        private void btTroLai_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void frmQuanLiNhaCungCap_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmMain.listForm.Remove(this);
        }
    }
}
